package org.example.lanchain.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.example.lanchain.bean.User;

import java.time.LocalDateTime;

/**
 * 用户Mapper接口
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
    
    /**
     * 根据用户名查找用户
     */
    User findByUsername(@Param("username") String username);
    
    /**
     * 根据邮箱查找用户
     */
    User findByEmail(@Param("email") String email);
    
    /**
     * 更新最后登录时间
     */
    @Update("UPDATE users SET last_login_time = #{loginTime} WHERE id = #{userId}")
    int updateLastLoginTime(@Param("userId") Long userId, @Param("loginTime") LocalDateTime loginTime);
    
    /**
     * 更新登录失败次数
     */
    @Update("UPDATE users SET login_fail_count = #{failCount} WHERE id = #{userId}")
    int updateLoginFailCount(@Param("userId") Long userId, @Param("failCount") Integer failCount);
    
    /**
     * 锁定账户
     */
    @Update("UPDATE users SET lock_time = #{lockTime}, status = 'banned' WHERE id = #{userId}")
    int lockAccount(@Param("userId") Long userId, @Param("lockTime") LocalDateTime lockTime);
    
    /**
     * 解锁账户
     */
    @Update("UPDATE users SET lock_time = NULL, login_fail_count = 0, status = 'active' WHERE id = #{userId}")
    int unlockAccount(@Param("userId") Long userId);
} 